package ctn.util.jmesa;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.jmesa.limit.Filter;
import org.jmesa.limit.FilterSet;
import org.jmesa.limit.Limit;
import org.jmesa.limit.Sort;
import org.jmesa.limit.SortSet;
import org.jmesa.util.BeanUtils;




public class TableFacadeUtils {


	public static PageFilter getPageFilter(Limit limit, Class rowObjectType) {
		PageFilter commonFilter = new PageFilter();
		FilterSet filterSet = limit.getFilterSet();
		Collection<Filter> filters = filterSet.getFilters();
		List<DTOFilter> wsFilters = new ArrayList<DTOFilter>();
		for (Filter filter : filters) {
			String property = filter.getProperty();
			Class type = null;
			try {
				type = BeanUtils.getPropertyType(rowObjectType, property);
			} catch (Exception e) {
				e.printStackTrace();
				type = String.class;
			}
			//log.debug("Property="+property+"    TYPE="+type);
			String value = filter.getValue();
			DTOFilter f = new DTOFilter(property, null);

			//f.setProperty(property);

			if(type.equals(Long.class)||type.equals(long.class)){
				f.setFieldType(FieldType.NUMBER);
				f.setOperationType(OperationType.EQUAL);	
				f.setValue(Long.valueOf(value));
				wsFilters.add(f);
			}else if(type.equals(Integer.class)){
				f.setFieldType(FieldType.NUMBER);
				f.setOperationType(OperationType.EQUAL);	
				f.setValue(Integer.valueOf(value));
				wsFilters.add(f);				
			}else if(type.equals(Double.class)){
				f.setFieldType(FieldType.NUMBER);
				f.setOperationType(OperationType.EQUAL);	
				f.setValue(Double.valueOf(value));
				wsFilters.add(f);
			}else if(type.equals(Float.class)){
				f.setFieldType(FieldType.NUMBER);
				f.setOperationType(OperationType.EQUAL);	
				f.setValue(Float.valueOf(value));
				wsFilters.add(f);				
			}else if(type.equals(Calendar.class)||type.equals(Date.class)){
				f.setFieldType(FieldType.DATE);
				f.setOperationType(OperationType.BETWEEN);	
				try {
					String dateStr = String.valueOf(value);
					Calendar cal = null;
					if(dateStr.matches(DateFormatConstant.DATE_CHS_REG)){
						cal = DateUtils.toCalendar(DateUtils.parse(DateFormatConstant.DATE_CHS,dateStr));
					}
					else if(dateStr.matches(DateFormatConstant.DATE_CHT_REG)){
						cal = DateUtils.toCalendar(DateUtils.parse(DateFormatConstant.DATE_CHT,dateStr));
					}
					else{
						cal = DateUtils.toCalendar(DateUtils.parse(DateFormatConstant.DATE_ENG,dateStr));
					}

					f.setLoValue(cal.clone());
					cal.add(Calendar.DAY_OF_MONTH, 1);
					f.setHiValue(cal);
					wsFilters.add(f);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}else if(type.equals(Boolean.class)||type.equals(boolean.class)||type.equals(char.class)){
				f.setOperationType(OperationType.EQUAL);	
				f.setValue(value);
				wsFilters.add(f);
			}else{
				f.setValue(value);
				f.setFieldType(FieldType.STRING);
				f.setOperationType(OperationType.LIKE);
				wsFilters.add(f);
			}

		}
		commonFilter.setFilters(wsFilters);
		return commonFilter;
	}

	public static PageSorter getPageSorter(Limit limit) {
		PageSorter commonSort = new PageSorter();
		SortSet sortSet = limit.getSortSet();
		Collection<Sort> sorts = sortSet.getSorts();
		if(sorts!=null){
			List<Sort> sortedList = new ArrayList<Sort>();
			sortedList.addAll(sorts);
			Collections.sort(sortedList);
			
			List<DTOSorter> wsSorts = new ArrayList<DTOSorter>();
			for (Sort sort : sortedList) {
				String property = sort.getProperty();
				String order = sort.getOrder().toParam();
				DTOSorter s = new DTOSorter(property, order);
				wsSorts.add(s);
			}
	
			commonSort.setSorts(wsSorts);
		}
		return commonSort;
	}

    


}
